package com.dwj.auto.comment.common.wechat.handler;

import com.dwj.auto.comment.common.utils.CodeUtil;
import com.dwj.auto.comment.dao.entity.SubscribeUser;
import com.dwj.auto.comment.dao.entity.User;
import com.dwj.auto.comment.model.wechat.WxMessage;
import com.dwj.auto.comment.service.ISubscribeUserService;
import com.dwj.auto.comment.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @ClassName SubscribeListener
 * @Description 关注事件监听者
 * @Author dwjian
 * @Date 2020/12/26 19:25
 */
@Slf4j
@Component
public class SubscribeHandler extends WxEventMessageHandler implements WxMessageHandler {

    @Resource
    private IUserService userService;
    @Resource
    private ISubscribeUserService subscribeUserService;

    @Override
    public String dealMessage(WxMessage message) {
        //记录订阅用户
        SubscribeUser subscribeUser = new SubscribeUser();
        String id = CodeUtil.uuid();
        subscribeUser.setId(id);
        subscribeUser.setOpenId(message.getOpenId());
        subscribeUser.setSubDate(new Date());
        subscribeUser.setStatus(1);//已关注
        subscribeUser.setIsDistribution(0);
        SubscribeUser localSubUser = subscribeUserService.insertOrReSubscribe(subscribeUser);
        //如果已经分配过账号,则激活账号
        if(localSubUser.getIsDistribution() == 1 && StringUtils.isNotEmpty(localSubUser.getUserId())){
            userService.lambdaUpdate().eq(User::getId, localSubUser.getUserId()).set(User::getStatus, 0).update();
        }
        return  "人生如果正青年，多学知识多挣钱！[加油]";
    }
}
